package com.bdqn.business.academic.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.business.academic.dao.ChapterMapper;
import com.bdqn.business.academic.dao.UpperMapper;
import com.bdqn.entity.academic.Ask;
import com.bdqn.entity.academic.Chapter;
import com.bdqn.entity.academic.Upper;
import com.bdqn.response.PageResult;
import com.bdqn.response.Result;
import com.bdqn.response.ResultCode;
import com.bdqn.utils.UploadUtils;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UpperService {

    @Autowired
    private UpperMapper upperMapper;

    @Autowired
    private ChapterMapper chapterMapper;

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    public Result getList(Map<String, Object> map) {
        Integer page = Integer.parseInt(map.get("page").toString());
        Integer size = Integer.parseInt(map.get("size").toString());

        QueryWrapper<Upper> queryWrapper = new QueryWrapper<>();

        queryWrapper.orderByDesc("create_time");

        IPage<Upper> singleIPage = new Page<>(page,size);

        IPage<Upper> result = upperMapper.selectPage(singleIPage, queryWrapper);

        PageResult<Upper> pageResult = new PageResult<>(result.getTotal(),result.getRecords());

        return new Result(ResultCode.SUCCESS,pageResult);
    }

    public Result upload(MultipartFile file) throws IOException {
        String uploadFile = UploadUtils.createUploadFile(fastFileStorageClient, file);
        String url = "http://47.97.77.138:8888/" + uploadFile;

        Upper upper = new Upper();
        upper.setCreateTime(new Date());
        upper.setUpperUrl(url);
        upper.setStatus("1");//启用
        upperMapper.insert(upper);

        return new Result(ResultCode.SUCCESS,upper);
    }

    public Result saveUpper(Upper upper) {
        Chapter chapter = chapterMapper.selectById(upper.getChapterId());
        upper.setChapterName(chapter.getName());
        upperMapper.updateById(upper);
        return new Result(ResultCode.SUCCESS);
    }

    public Result getUpperListByChapters(Map<String, Object> map) {
        List<String> chapterIds = (List<String>) map.get("chapterIds");
        List<Upper> list = new ArrayList<>();
        for (String chapterId : chapterIds) {
            QueryWrapper<Upper> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("chapter_id",chapterId);
            List<Upper> uppers = upperMapper.selectList(queryWrapper);
            list.addAll(uppers);
        }
        return new Result(ResultCode.SUCCESS,list);
    }
}
